package com.Mobile;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class Mobile extends Activity {
    private String activeUsername;

    private Button btnLogin;
    private Button btnLogout;
    private EditText txtUsername;
    private EditText txtPassword;
    private TextView lblResult;
    private TextView lblActiveUser;
    private TextView lblTitle;
    private ImageButton btnHome;
    private ImageButton btnRoutes;
    private ImageButton btnMessages;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loginListener();
    }

    /**
     * Called when the activity is first created.
     */
    private void loginListener() {
        txtUsername = (EditText) findViewById(R.id.txtUsername);
        txtPassword = (EditText) findViewById(R.id.txtPassword);
        lblResult = (TextView) findViewById(R.id.lblResult);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                if (checkLogin(txtUsername.getText().toString(), txtPassword.getText().toString())) {
                    activeUsername = "Ik_Probeer_In_Te_Loggen@hotmail.com"; //txtUsername.getText().toString();
                    createHome();
                    txtPassword.setText("");
                    txtUsername.setText("");
                } else {
                    txtPassword.setText("");
                    txtUsername.setText("");
                    lblResult.setVisibility(1);
                    lblResult.setText("Foute inloggegevens voer opnieuw in!");
                }
            }
        });
    }

    /**
     * Checks the username and password and, when valid,
     * returns true and redirects the user back to login-page.
     */
    private boolean checkLogin(String username, String password) {
        return username.equalsIgnoreCase("t") && password.equalsIgnoreCase("t");
    }

    /**
     * Logs the user out and redirects the user back to login-page.
     */
    private void logout() {
        setContentView(R.layout.main);
        loginListener();
    }

    /**
     * Creates listeners for all every button on the current page.
     */
    private void createListeners(){
        btnHome = (ImageButton) findViewById(R.id.btnHome);
        btnHome.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                changePage("home");
            }
        });

        btnRoutes = (ImageButton) findViewById(R.id.btnRoutes);
        btnRoutes.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                changePage("routes");
            }
        });

        btnMessages = (ImageButton) findViewById(R.id.btnMessages);
        btnMessages.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                changePage("messages");
            }
        });

        btnLogout = (Button) findViewById(R.id.btnLogout);
        btnLogout.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                logout();
            }
        });
    }

    /**
     * Redirects the user to the selected page.
     */
    private void changePage(String s) {
        if(s.equalsIgnoreCase("messages")){
            createMessages();
        }else if(s.equalsIgnoreCase("routes")){
            createRoutes();
        }else if(s.equalsIgnoreCase("help")){
            createHelp();
        }else if(s.equalsIgnoreCase("home")){
            createHome();
        }
    }

    private void createHome() {
        setContentView(R.layout.home);
        lblTitle = (TextView) findViewById(R.id.lblTitle);
        lblActiveUser = (TextView) findViewById(R.id.lblActiveUser);

        lblTitle.setText("Home");
        lblActiveUser.setText(activeUsername);

        createListeners();
    }

    private void createHelp() {
        setContentView(R.layout.help);
        lblTitle = (TextView) findViewById(R.id.lblTitle);
        lblActiveUser = (TextView) findViewById(R.id.lblActiveUser);

        lblTitle.setText("Help");
        lblActiveUser.setText(activeUsername);

        createListeners();
    }

    private void createRoutes() {
        setContentView(R.layout.routes);
        lblTitle = (TextView) findViewById(R.id.lblTitle);
        lblActiveUser = (TextView) findViewById(R.id.lblActiveUser);

        lblTitle.setText("Routes");
        lblActiveUser.setText(activeUsername);

        createListeners();
    }

    private void createMessages() {
        setContentView(R.layout.messages);
        lblTitle = (TextView) findViewById(R.id.lblTitle);
        lblActiveUser = (TextView) findViewById(R.id.lblActiveUser);

        lblTitle.setText("Messages");
        lblActiveUser.setText(activeUsername);

        createListeners();
    }
}